﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Entity.IRepository.Project;
using Entity.ProjectEnt;
using Logic.Repository.Project;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.SystemManage;
using ZFine.Repository.SystemManage;

namespace ZFine.Application.Project
{
    public class GudaoCarApp
    {
        private IGudaoCarRepository service = new GudaoCarRepository();
        private IOperationGudaoCarRepository operService = new OperationGudaoCarRepository();
        private IPersonSalaryRepository perService = new PersonSalaryRepository();
        private PositionRecordApp positoonapp = new PositionRecordApp();
        private CarriageRepository carriageservice = new CarriageRepository();
        private PathwayRepository patywayservice = new PathwayRepository();
        private IUserRepository userService = new UserRepository();
        private PositionRecordApp posService = new PositionRecordApp();
        private ICustomerAndThingsNameRepository servicepingming = new CustomerAndThingsNameRepository();
        private ICarriageBoxRepository carriageBoxservice = new CarriageBoxRepository();
        //private 
        public List<GudaoCarEntity> GetList()
        {
            return service.IQueryable().ToList();
        }
        public List<GudaoCarEntity> GetList(Expression<Func<GudaoCarEntity, bool>> predicate)
        {
            return service.IQueryable(predicate).ToList();
        }

        public GudaoCarEntity GetNoTrackObj(Expression<Func<GudaoCarEntity, bool>> predicate)
        {
            return service.FindEntityAsNoTracking(predicate);
        }


        public List<GudaoCarEntity> GetListInclude(Expression<Func<GudaoCarEntity, bool>> predicate)
        {
            return service.GetIncludeList(predicate);
        }

        public List<GudaoCarEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.BelongPathway.Name.Contains(keyword));
                expression = expression.And(t => t.State == true);
                return service.GetIncludeList(expression);
                //return service.FindList(expression,pagination);
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 接车查询方法
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<GudaoCarEntity> GetListArrive(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.BelongPathway.Name.Contains(keyword));
                expression = expression.And(t => t.State == true);
                //return service.GetIncludeList(expression);
                return service.FindList(expression,pagination);
            }
            else
            {
                expression = expression.And(t => t.State == true);
                //return service.GetIncludeList(expression);
                return service.FindList(expression, pagination);
            }

        }


        /// <summary>
        /// 车辆查询
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<GudaoCarEntity> GetListCar(Pagination pagination, string gudaoName,string carId)
        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(gudaoName))
            {
                expression = expression.And(t => t.BelongPathway.Name.Contains(gudaoName));
            }

            if (!string.IsNullOrEmpty(carId))
            {
                expression = expression.And(f => f.BelongCarriage.CarriageID.Contains(carId));
            }
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        /// <summary>
        /// 起票查询方法
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<GudaoCarEntity> GetListQiPiao(Pagination pagination, string keyword,string StartTime,string EndTime,string carId,string arriveStation, string customerName, string pingming)

        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.BelongPathway.Name.Contains(keyword));
            }

            if (!string.IsNullOrEmpty(arriveStation))
            {
                expression = expression.And(t => t.ArriveStation== arriveStation);
            }
            if (!string.IsNullOrEmpty(customerName))
            {
                expression = expression.And(t => t.Customers == customerName);
            }
            if (!string.IsNullOrEmpty(pingming))
            {
                expression = expression.And(t => t.ThingsName == pingming);
            }

            if (!string.IsNullOrEmpty(carId))
            {
                expression = expression.And(t => t.BelongCarriage.CarriageID.Contains(carId));
            }
            if (!string.IsNullOrEmpty(StartTime))
            {
                DateTime sTime = Convert.ToDateTime(StartTime);
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.QiPiaoTime >= sTime);
                    expression = expression.And(f => f.QiPiaoTime < eTime);
                }
                else
                {
                    expression = expression.And(f => f.QiPiaoTime >= sTime);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.QiPiaoTime < eTime);
                }

            }
            //expression = expression.And(t => t.State == true);
            expression = expression.And(f => f.isQiPiao == true);
            //expression = expression.And(f => f.QiPiaoOper.OperationType == 1);
            return service.FindList(expression, pagination);
        }
        /// <summary>
        /// 排车查询
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<GudaoCarEntity> GetListPass(Pagination pagination, string keyword,string StartTime,string EndTime,string carId,string customerName)
        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            //if (!string.IsNullOrEmpty(keyword))
            //{
            //    expression = expression.And(f => f.BelongPathway.Name.Contains(keyword));
            //    //return service.GetIncludeList(expression);
            //}
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(f => f.ThingsName.Contains(keyword));
                //return service.GetIncludeList(expression);
            }
            if (!string.IsNullOrEmpty(customerName))
            {
                expression = expression.And(f => f.Customers.Contains(customerName));
            }
            if (!string.IsNullOrEmpty(carId))
            {
                expression = expression.And(f => f.BelongCarriage.CarriageID.Contains(carId));
            }
            if (!string.IsNullOrEmpty(StartTime))
            {
                DateTime sTime = Convert.ToDateTime(StartTime);
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.AccomplishPaicheTime >= sTime);
                    expression = expression.And(f => f.AccomplishPaicheTime < eTime);
                }
                else
                {
                    expression = expression.And(f => f.AccomplishPaicheTime >= sTime);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.AccomplishPaicheTime < eTime);
                }

            }
            expression = expression.And(f => f.State == false);
            expression = expression.And(f => f.isAccomplishPaiche == true);
            pagination.rows = 100000;
            List<GudaoCarEntity> list= service.FindList(expression, pagination);
            return list;

        }

        /// <summary>
        /// 卸后利用查询
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<GudaoCarEntity> GetListRecovery(Pagination pagination, string keyword, string StartTime, string EndTime, string carId)
        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.BelongPathway.Name.Contains(keyword));
            }

            if (!string.IsNullOrEmpty(carId))
            {
                expression = expression.And(t => t.BelongCarriage.CarriageID.Contains(carId));
            }
            if (!string.IsNullOrEmpty(StartTime))
            {
                DateTime sTime = Convert.ToDateTime(StartTime);
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.RecoveryTime >= sTime);
                    expression = expression.And(f => f.RecoveryTime < eTime);
                }
                else
                {
                    expression = expression.And(f => f.RecoveryTime >= sTime);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.RecoveryTime < eTime);
                }

            }
            //expression = expression.And(t => t.State == true);
            expression = expression.And(f => f.IsRecovery == true);
            //expression = expression.And(f => f.RecoveryOper.OperationType == 11);
            return service.FindList(expression, pagination);
        }

        /// <summary>
        /// 向股道中添加车辆
        /// </summary>
        /// <param name="objlist">添加信息</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public int SubmitAddCarToGudao(List<CarAndgudao> objlist, ref string msg)
        {
            Pagination pagination2 = new Pagination
            {
                sord = "F_CreatorTime desc",
                sidx = "F_CreatorTime",
                page = 1,
                rows = 1000000000
            };

            int code = 0;
            bool isexitcar = false;
            for (int i = 0; i < objlist.Count; i++)
            {
                var carriageid = objlist[i].chehao;
                var car = carriageservice.FindEntity(z => z.CarriageID == carriageid && z.State == true);
                if (car != null)
                {
                    msg += string.Format("{0},此车号已经存在！", objlist[i].chehao);
                    isexitcar = true;
                }
            }
            if (isexitcar)
            {
                code = -3;
            }
            else
            {

                bool isRepetitionnumber = false;
                for (int i = 0; i < objlist.Count; i++)
                {
                    for (int j = i + 1; j < objlist.Count; j++)
                    {
                        if (objlist[j].chehao == objlist[i].chehao)
                        {
                            isRepetitionnumber = true;
                            break;
                        }
                    }
                }
                if (isRepetitionnumber)
                {
                    msg = "添加列存在相同的车号";
                    return -4;
                }

                List<GudaoCarEntity> addgudaocarlist = new List<GudaoCarEntity>();
                OperationGudaoCarEntity operation = new OperationGudaoCarEntity();
                operation.Create();
                operation.State = true;
                operation.OperationTime = DateTime.Now;
                operation.OperationType = 0;
                var LoginInfo = OperatorProvider.Provider.GetCurrent();
                if (LoginInfo != null)
                {
                    operation.OperationPersion = new UserEntity() { F_Id = LoginInfo.UserId };
                }
                var thispaiban = positoonapp.GetForm(f => f.State == true);
                if (thispaiban != null)//有效班期id
                {
                    operation.Banci = thispaiban.F_Id;
                }
                string pathwayid = objlist[0].gudao;
                var gudao = patywayservice.FindEntityAsNoTracking(z => z.F_Id == pathwayid);
                var MaxCarGudaoIndex =
                    service.FindEntityAsNoTrackinglist(f => f.State == true && f.BelongPathway.F_Id == pathwayid);
                int maxindex =0;
                if (MaxCarGudaoIndex != null && MaxCarGudaoIndex.Count > 0)
                {
                    maxindex = Convert.ToInt32((from V in MaxCarGudaoIndex select V.CarGudaoIndex).Max());
                }
                for (int i = 0; i < objlist.Count; i++)
                {
                    maxindex += 1;
                    var temp = new GudaoCarEntity();
                    temp.Create();
                    temp.ThingsName = objlist[i].pingming;
                    temp.Fenhao = objlist[i].fenghao;
                    temp.ArriveStation = objlist[i].daofazhan;
                    temp.ArriveDateTime = DateTime.Now;
                    temp.UnitNumber = (!string.IsNullOrEmpty(objlist[i].jianshu)?Convert.ToInt32(objlist[i].jianshu):0);
                    temp.Weight = Convert.ToInt32(objlist[i].zhongliang);
                    temp.Remark = objlist[i].beizhu;
                    temp.State = true;
                    temp.Customers = objlist[i].kehu;
                    temp.CarGudaoIndex = maxindex;
                    var addcar = new CarriageEntity();
                    addcar.Create();
                    addcar.ThingsName= objlist[i].pingming;
                    addcar.FenHao= objlist[i].fenghao;
                    addcar.ArriveTime = DateTime.Now;
                    addcar.ArriveStation = objlist[i].daofazhan;//新增字段
                    addcar.Customers= objlist[i].kehu;
                    addcar.Remark= objlist[i].beizhu;
                    addcar.Weight= Convert.ToInt32(objlist[i].zhongliang);
                    addcar.ThingsCount = (!string.IsNullOrEmpty(objlist[i].jianshu) ? Convert.ToInt32(objlist[i].jianshu) : 0);
                    addcar.State = true;
                    addcar.CarriageID = objlist[i].chehao;
                    addcar.CarType = objlist[i].chexing;
                    addcar.Capacity = (!string.IsNullOrEmpty(objlist[i].zaizhong)?Convert.ToDecimal(objlist[i].zaizhong):0);
                    addcar.Remark = objlist[i].beizhu;
                    temp.BelongCarriage = addcar;
                    if (gudao != null)
                    {
                        addcar.PathWay = gudao.Name;
                        temp.BelongPathway = gudao;
                    }
                    //maxindex++;
                    //temp.BelongPathway=new PathwayEntity(){F_Id =  };
                    addgudaocarlist.Add(temp);
                }
                code = service.insertGudaoCarList(addgudaocarlist, operation);
                if (code >= 0)
                {
                    List<CarriageInfoLogEntity> carriageInfoLogList=new List<CarriageInfoLogEntity>();
                    List<CarriageBoxEntity> carriageBoxList = new List<CarriageBoxEntity>();
                    addgudaocarlist.ForEach(f =>
                    {
                        var jsonStr = f.BelongCarriage.ToJson();
                        CarriageInfoLogEntity tempEntityt = Json.ToObject<CarriageInfoLogEntity>(jsonStr);
                        if (tempEntityt != null && !tempEntityt.F_Id.IsEmpty())
                        {
                            tempEntityt.Create();
                            tempEntityt.OperationType = "添加车辆";
                            carriageInfoLogList.Add(tempEntityt);
                            List<CarriageBoxEntity> tempCarBox =
                                carriageBoxservice.FindList(t => t.InitBelongCarriageId == tempEntityt.CarriageID, pagination2);
                            if (tempCarBox != null && tempCarBox.Count > 0)
                            {
                                tempCarBox.ForEach(z =>
                                {
                                    z.ThingsName = tempEntityt.ThingsName;
                                    z.InitBelongTime = DateTime.Now;
                                    z.InitWeight = tempEntityt.Weight.ToString();
                                });
                                carriageBoxList.AddRange(tempCarBox);
                            }
                        }
                    });
                    carriageBoxservice.UpdateCarBoxAndInsertCarriageInfoLog(carriageBoxList, carriageInfoLogList);
                    msg = "添加车辆成功";
                }
                else
                {
                    msg = "添加车辆失败，请稍后重试";
                }
            }
            return code;
        }

        public List<GudaoCarEntity> GetListD(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            expression = expression.And(t => t.State == true);
            return service.FindList(expression, pagination);
        }
        public GudaoCarEntity GetForm(string keyValue)
        {
            return service.FindEntity(f => f.F_Id == keyValue);
        }
        public int DeleteForm(string keyValue)
        {
            var local = service.FindEntity(keyValue);
            if (local != null)
            {
                local.State = false;
                return service.Update(local);
            }
            else
            {
                return -1;
            }
        }
        public int SubmitForm(GudaoCarEntity ServiceEntity, string keyValue,decimal Capacity,string carType,string carriageID)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                var expression = ExtLinq.True<GudaoCarEntity>();
                expression = expression.And(f => f.F_Id == keyValue && f.State == true);
                var result = service.FindEntity(expression);
                var resCarriage = carriageservice.FindEntity(result.BelongCarriage.F_Id);
                //if (ServiceEntity.Weight > resCarriage.Capacity)
                //{
                //    return -2;//物品重量超过载重
                //}
                //else
                //{
                //    result.Weight = ServiceEntity.Weight;
                //}
                if (!string.IsNullOrEmpty(carType))
                {
                    resCarriage.CarType=carType;
                }
                if (resCarriage.CarriageID != carriageID)
                {
                    var repeatCarriageIDCar = carriageservice.FindEntity(f => f.CarriageID == carriageID && f.State == true);
                    if (repeatCarriageIDCar != null)
                        return -10;//修改车id 有重复项
                    resCarriage.CarriageID = carriageID;
                };
                resCarriage.Capacity =Capacity;

                carriageservice.Update(resCarriage);
                if (result.Weight != ServiceEntity.Weight)//当车中的重量发生改变的时候，修改车中箱子的重量（每个箱子的重量==车的当前重量）
                {
                    Pagination pagination2 = new Pagination();
                    pagination2.sord = "F_CreatorTime desc";
                    pagination2.sidx = "F_CreatorTime";
                    pagination2.page = 1;
                    pagination2.rows = 1000000000;
                    List<CarriageBoxEntity> carriageBoxEntitieList =
                        carriageBoxservice.FindList(f => f.ChangeBelongCarriageId == resCarriage.CarriageID, pagination2);
                    if (carriageBoxEntitieList != null && carriageBoxEntitieList.Count > 0)
                    {
                        carriageBoxEntitieList.ForEach(f => { f.ChangeWeight = ServiceEntity.Weight.ToString(); });
                        carriageBoxservice.UpdateList(carriageBoxEntitieList);
                    }
                }
                result.Weight = ServiceEntity.Weight;
                result.UnitNumber = (ServiceEntity.UnitNumber == null ? 0 : ServiceEntity.UnitNumber);
                result.ArriveStation = ServiceEntity.ArriveStation;
                result.Customers = ServiceEntity.Customers;
                result.ThingsName = ServiceEntity.ThingsName;
                result.Fenhao = ServiceEntity.Fenhao;
                result.Remark = ServiceEntity.Remark;
                ServiceEntity.Modify(keyValue);
                return service.Update(result);
                
            }
            else
            {
                return -1;//股道信息错误
            }
           
        }
/// <summary>
/// ///////////////////////////////////统计运量方法/////////////////////////////////////////////////////////////////////
/// </summary>
/// <param name="keyValue"></param>
/// <returns></returns>
        public int YunLiang(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            //var LoginInfo = posService.GetForm(t => t.State == true);
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyValue))
            {
                expression = expression.And(t => t.F_Id == keyValue);
                expression = expression.And(t => t.State == true);
                var result = service.FindEntity(expression);
                if (result.isAccomplishStatistics!=true)
                {
                    result.isAccomplishStatistics = true;
                    result.AccomplishStatisticsTime = DateTime.Now;
                }
                else
                {
                    return -4;
                }
                if (result.Weight == null)
                {
                    result.Weight = 0;
                }

                //获取当前上班的人员
                var positionrecord = positoonapp.GetForm(f => f.State == true);
                //获取操作的车辆信息
                var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
                //插入操作数据
                OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
                oper.Create();
                oper.State = true;
                oper.OperationType = 3;
                oper.Banci = positionrecord.F_Id;
                oper.F_CreatorTime = DateTime.Now;
                oper.IsRollBack = false;
                var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
                //oper.OperationPersion = new UserEntity();
                //oper.OperationPersion.F_Id = LoginInfo.UserId;
                oper.OperationPersion = perO;
                result.Operation = new OperationGudaoCarEntity();
                result.Operation = oper;
                //批量插入运量工资数据
                List<PersonalSalaryEntity> perSalaryList = new List<PersonalSalaryEntity>();
                //插入工资
                ////给当前上班的人员加运量 除了货运员
                
                //暂定系数是0.15
                decimal? xishu= Convert.ToDecimal(0.15);
                for (int i = 0; i < 8; i++)
                {
                    PersonalSalaryEntity pSalary = new PersonalSalaryEntity();
                    pSalary.Create();
                    //创建一个临时变量存 是否是对位的吨数
                     decimal? weight=0;
                    //判断属于哪根股道 有没有开启对位
                    if (result.BelongPathway.IsCounterpoint == false)
                    {
                        pSalary.Weight = result.Weight;
                        pSalary.DWeight = 0;
                        weight = result.Weight;

                    }
                    else
                    {
                        pSalary.Weight = result.Weight / 2;
                        pSalary.DWeight = 0;
                        weight = result.Weight/2;
                    }

                    switch (i)
                    {
                        case 0:
                            var per0 = userService.FindEntityAsNoTracking(f=>f.F_Id==positionrecord.DispatcherUser.F_Id);
                            pSalary.Persion = per0;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 1:
                            var per1 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.DiaochezhangUser.F_Id);
                            pSalary.Persion = per1;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 2:
                            var per2 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.TougouyuanUser.F_Id);
                            pSalary.Persion = per2;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 3:
                            var per3 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.ZhongzhuanyuanUser.F_Id);
                            pSalary.Persion = per3;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 4:
                            var per4 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.SijiUser.F_Id);
                            pSalary.Persion = per4;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 5:
                            var per5 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.FusijiUser.F_Id);
                            pSalary.Persion = per5;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 6:
                            var per6 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.daokouyuanUser1.F_Id);
                            pSalary.Persion = per6;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 7:
                            var per7 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.daokouyuanUser2.F_Id);
                            pSalary.Persion = per7;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                    }

                    if (result.IsRecovery==true)
                    {
                        pSalary.Remake = "卸后利用";
                    }
                    pSalary.EveryCarID = carriage;
                    pSalary.F_CreatorTime = DateTime.Now;
                    pSalary.OperationTime = DateTime.Now;
                    pSalary.State = true;
                    pSalary.SalaryType = 0;
                    perSalaryList.Add(pSalary);
                }
                var resPer = perService.InsertPer(perSalaryList, oper);
                var resGudao = service.Update(result);
                if (resGudao > 0)
                {
                    if (resPer > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return -2;
                }
            }
            else
            {
                return -3;
            }

        }
//////////////////////////////////////////////取消统计运量////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 取消运量
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public int CancelYunLiang(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyValue))
            {
                expression = expression.And(t => t.F_Id == keyValue);
                expression = expression.And(t => t.State == true);
                var result = service.FindEntity(expression);
                if (result.isAccomplishStatistics == true)
                {
                    result.isAccomplishStatistics = false;
                    result.AccomplishStatisticsTime = DateTime.Now;
                }
                else
                {
                    return -4; //没有统计过运量！
                }
                if (result.Weight == null)
                {
                    result.Weight = 0;
                }
                //获取当前上班的人员
                var positionrecord = positoonapp.GetForm(f => f.State == true);
                var postInfo = posService.GetForm(f=>f.F_Id==result.Operation.Banci);
                if (positionrecord.BanciUser != postInfo.BanciUser)
                {
                    return -5;//返回 当前班次与统计运量班次不对应！不能进行此操作
                } 
                //获取操作的车辆信息
                var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
                //找到上次统计运量时的一条数据中的吨数和工资数
                var experssion2 = ExtLinq.True<PersonalSalaryEntity>();
                experssion2 = experssion2.And(f => f.OperationGudaoCarEntity.F_Id == result.Operation.F_Id);
                experssion2 = experssion2.And(f => f.OperationGudaoCarEntity.OperationType == 3);
                var personInfo = perService.FindEntityAsNoTracking(experssion2); 
                //插入操作数据
                OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
                oper.Create();
                oper.State = true;
                oper.OperationType = 8;
                oper.Banci = positionrecord.F_Id; //进行此操作的班次
                oper.F_CreatorTime = DateTime.Now;
                oper.IsRollBack = true;
                oper.RollbackTime = DateTime.Now;
                var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
                //oper.OperationPersion = new UserEntity();
                //oper.OperationPersion.F_Id = LoginInfo.UserId;
                oper.OperationPersion = perO;//操作员

               
                //更新gudaocar的操作数据
                result.Operation = new OperationGudaoCarEntity();
                result.Operation = oper;
                //批量插入运量工资数据
                List<PersonalSalaryEntity> perSalaryList = new List<PersonalSalaryEntity>();
                
                //暂定系数是0.15
                decimal? xishu = Convert.ToDecimal(0.15);
                //插入工资
                ////给对该车统计过运量的人员减运量 除了货运员 
                for (int i = 0; i < 8; i++)
                {
                    PersonalSalaryEntity pSalary = new PersonalSalaryEntity();
                    pSalary.Create();
                  
                    switch (i)
                    {
                        case 0:
                            var per0 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.DispatcherUser.F_Id);
                            pSalary.Persion = per0;
                            pSalary.EveryTimeSalary =
                               (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 1:
                            var per1 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.DiaochezhangUser.F_Id);
                            pSalary.Persion = per1;
                            pSalary.EveryTimeSalary =
                                (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * (-1);
                            break;
                        case 2:
                            var per2 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.TougouyuanUser.F_Id);
                            pSalary.Persion = per2;
                            pSalary.EveryTimeSalary =
                                (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 3:
                            var per3 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.ZhongzhuanyuanUser.F_Id);
                            pSalary.Persion = per3;
                            pSalary.EveryTimeSalary =
                                (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 4:
                            var per4 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.SijiUser.F_Id);
                            pSalary.Persion = per4;
                            pSalary.EveryTimeSalary =
                                (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 5:
                            var per5 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.FusijiUser.F_Id);
                            pSalary.Persion = per5;
                            pSalary.EveryTimeSalary =
                                (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 6:
                            var per6 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.daokouyuanUser1.F_Id);
                            pSalary.Persion = per6;
                            pSalary.EveryTimeSalary =
                                (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 7:
                            var per7 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.daokouyuanUser2.F_Id);
                            pSalary.Persion = per7;
                            pSalary.EveryTimeSalary =
                                (personInfo.Weight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                    }
                    //判断属于哪根股道 有没有开启对位
                    if (result.BelongPathway.IsCounterpoint == false)
                    {
                        pSalary.Weight = personInfo.Weight * -1;
                        pSalary.DWeight = 0;
                    }
                    else
                    {
                        pSalary.Weight = personInfo.Weight * -1;
                        pSalary.DWeight = 0;
                    }
                    if (result.IsRecovery == true)
                    {
                        pSalary.Remake = "卸后利用";
                    }
                    pSalary.EveryCarID = carriage;
                    pSalary.F_CreatorTime = DateTime.Now;
                    pSalary.OperationTime = DateTime.Now;
                    pSalary.SalaryType = 0;
                    pSalary.State = true;
                  

                    perSalaryList.Add(pSalary);
                }
                var resPer = perService.InsertPer(perSalaryList, oper);
                var resGudao = service.Update(result);
                if (resGudao > 0)
                {
                    if (resPer > 0)
                    {
                        return 1; //操作成功
                    }
                    else
                    {
                        return -1;//修改工资数据失败
                    }
                }
                else
                {
                    return -2;//修改股道数据失败
                }
            }
            else
            {
                return -3;//没有找到对应车辆
            }

        }

////////////////////////////////////////////////////////////对位//////////////////////////////////////////////////////////    
        public int Duiwei(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            //var LoginInfo = posService.GetForm(t => t.State == true);
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyValue))
            {
                expression = expression.And(t => t.F_Id == keyValue);
                expression = expression.And(t => t.State == true);
                var result = service.FindEntity(expression);
                if (result.IsAccomplishduiwei != true)
                {
                    result.IsAccomplishduiwei = true;
                    result.AccomplishduiweiTime = DateTime.Now;
                }
                else
                {
                    return -4;
                }
                if (result.Weight == null)
                {
                    result.Weight = 0;
                }
                //获取当前上班的人员
                var positionrecord = positoonapp.GetForm(f => f.State == true);
                //获取操作的车辆信息
                var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
                //插入操作数据
                OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
                oper.Create();
                oper.State = true;
                oper.OperationType = 5;
                oper.Banci = positionrecord.F_Id;
                oper.F_CreatorTime = DateTime.Now;
                oper.IsRollBack = false;
                var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
                //oper.OperationPersion = new UserEntity();
                //oper.OperationPersion.F_Id = LoginInfo.UserId;
                oper.OperationPersion = perO;
                result.DuiWeiOper = new OperationGudaoCarEntity();
                result.DuiWeiOper = oper;
                //批量插入运量工资数据
                List<PersonalSalaryEntity> perSalaryList = new List<PersonalSalaryEntity>();
                //暂定系数是0.15
                decimal? xishu = Convert.ToDecimal(0.15);
                //插入工资
                ////给当前上班的人员加运量 除了货运员 
                for (int i = 0; i < 8; i++)
                {
                    PersonalSalaryEntity pSalary = new PersonalSalaryEntity();
                    pSalary.Create();
                    //创建一个临时变量存 是否是对位的吨数
                    decimal? weight = 0;
                    //判断属于哪根股道 有没有开启对位
                    if (result.BelongPathway.IsCounterpoint == true)
                    {
                        pSalary.Weight =0;
                        pSalary.DWeight =  result.Weight/2;
                        weight = result.Weight/2;
                    }
                    else
                    {
                        return -6;//该股道没有开启对位属性 不能进行对位操做
                    }
                    switch (i)
                    {
                        case 0:
                            var per0 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.DispatcherUser.F_Id);
                            pSalary.Persion = per0;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 1:
                            var per1 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.DiaochezhangUser.F_Id);
                            pSalary.Persion = per1;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 2:
                            var per2 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.TougouyuanUser.F_Id);
                            pSalary.Persion = per2;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 3:
                            var per3 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.ZhongzhuanyuanUser.F_Id);
                            pSalary.Persion = per3;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 4:
                            var per4 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.SijiUser.F_Id);
                            pSalary.Persion = per4;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 5:
                            var per5 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.FusijiUser.F_Id);
                            pSalary.Persion = per5;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 6:
                            var per6 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.daokouyuanUser1.F_Id);
                            pSalary.Persion = per6;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                        case 7:
                            var per7 = userService.FindEntityAsNoTracking(f => f.F_Id == positionrecord.daokouyuanUser2.F_Id);
                            pSalary.Persion = per7;
                            if (pSalary.Persion.Gongzixishu == null)
                            {
                                return -5;
                            }
                            pSalary.EveryTimeSalary =
                                weight * pSalary.Persion.Gongzixishu * xishu;
                            break;
                    }
                    if (result.IsRecovery == true)
                    {
                        pSalary.Remake = "卸后利用";
                    }
                    pSalary.EveryCarID = carriage;
                    pSalary.F_CreatorTime = DateTime.Now;
                    pSalary.OperationTime = DateTime.Now;
                    pSalary.State = true;
                    pSalary.SalaryType = 2;
                    perSalaryList.Add(pSalary);
                }
                var resPer = perService.InsertPer(perSalaryList, oper);
                var resGudao = service.Update(result);
                if (resGudao > 0)
                {
                    if (resPer > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return -1;
                    }
                }
                else
                {
                    return -2;
                }
            }
            else
            {
                return -3;
            }
        }
//////////////////////////////////////////////////取消对位////////////////////////////////////////////////////////
        public int CancelDuiwei(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyValue))
            {
                expression = expression.And(t => t.F_Id == keyValue);
                expression = expression.And(t => t.State == true);
                var result = service.FindEntity(expression);
                if (result.IsAccomplishduiwei == true)
                {
                    result.IsAccomplishduiwei = false;
                    result.AccomplishduiweiTime = DateTime.Now;
                }
                else
                {
                    return -4; //没有统计过运量！
                }
                if (result.Weight == null)
                {
                    result.Weight = 0;
                }
                //获取当前上班的人员
                var positionrecord = positoonapp.GetForm(f => f.State == true);
                //var postInfo = posService.GetForm(f => f.F_Id == result.Operation.Banci);
                var postInfo = posService.GetForm(f => f.F_Id == result.DuiWeiOper.Banci);
                if (positionrecord.BanciUser != postInfo.BanciUser)
                {
                    return -5;//返回 当前班次与统计运量班次不对应！不能进行此操作
                }
                //获取操作的车辆信息
                var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
                //找到上次统计运量时的一条数据中的吨数和工资数
                var experssion2 = ExtLinq.True<PersonalSalaryEntity>();
                experssion2 = experssion2.And(f => f.OperationGudaoCarEntity.F_Id == result.DuiWeiOper.F_Id);
                experssion2 = experssion2.And(f => f.OperationGudaoCarEntity.OperationType == 5);
                var personInfo = perService.FindEntityAsNoTracking(experssion2);
                //插入操作数据
                OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
                oper.Create();
                oper.State = true;
                oper.OperationType =10;
                oper.Banci = positionrecord.F_Id; //进行此操作的班次
                oper.F_CreatorTime = DateTime.Now;
                oper.IsRollBack = true;
                oper.RollbackTime = DateTime.Now;
                var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
                //oper.OperationPersion = new UserEntity();
                //oper.OperationPersion.F_Id = LoginInfo.UserId;
                oper.OperationPersion = perO;//操作员


                //更新gudaocar的操作数据
                result.DuiWeiOper = new OperationGudaoCarEntity();
                result.DuiWeiOper = oper;
                //批量插入运量工资数据
                List<PersonalSalaryEntity> perSalaryList = new List<PersonalSalaryEntity>();
                //暂定系数是0.15
                decimal? xishu = Convert.ToDecimal(0.15);
                //插入工资
                ////给对该车统计过运量的人员减运量 除了货运员 
                for (int i = 0; i < 8; i++)
                {
                    PersonalSalaryEntity pSalary = new PersonalSalaryEntity();
                    pSalary.Create();
                    switch (i)
                    {
                        case 0:
                            var per0 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.DispatcherUser.F_Id);
                            pSalary.Persion = per0;
                            pSalary.EveryTimeSalary =
                               (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 1:
                            var per1 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.DiaochezhangUser.F_Id);
                            pSalary.Persion = per1;
                            pSalary.EveryTimeSalary =
                                (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * (-1);
                            break;
                        case 2:
                            var per2 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.TougouyuanUser.F_Id);
                            pSalary.Persion = per2;
                            pSalary.EveryTimeSalary =
                                (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 3:
                            var per3 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.ZhongzhuanyuanUser.F_Id);
                            pSalary.Persion = per3;
                            pSalary.EveryTimeSalary =
                                (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 4:
                            var per4 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.SijiUser.F_Id);
                            pSalary.Persion = per4;
                            pSalary.EveryTimeSalary =
                                (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 5:
                            var per5 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.FusijiUser.F_Id);
                            pSalary.Persion = per5;
                            pSalary.EveryTimeSalary =
                                (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 6:
                            var per6 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.daokouyuanUser1.F_Id);
                            pSalary.Persion = per6;
                            pSalary.EveryTimeSalary =
                                (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                        case 7:
                            var per7 = userService.FindEntityAsNoTracking(f => f.F_Id == postInfo.daokouyuanUser2.F_Id);
                            pSalary.Persion = per7;
                            pSalary.EveryTimeSalary =
                                (personInfo.DWeight) * pSalary.Persion.Gongzixishu * xishu * -1;
                            break;
                    }
                    //判断属于哪根股道 有没有开启对位
                    if (result.BelongPathway.IsCounterpoint == false)
                    {
                        return -6;//该股道没有开启对位功能不能进行此操作
                    }
                    else
                    {
                        pSalary.DWeight = personInfo.DWeight * -1;
                        pSalary.Weight = 0;
                    }
                    if (result.IsRecovery == true)
                    {
                        pSalary.Remake = "卸后利用";
                    }
                    pSalary.EveryCarID = carriage;
                    pSalary.F_CreatorTime = DateTime.Now;
                    pSalary.OperationTime = DateTime.Now;
                    pSalary.SalaryType = 2;
                    pSalary.State = true;
                    perSalaryList.Add(pSalary);
                }
                var resPer = perService.InsertPer(perSalaryList, oper);
                var resGudao = service.Update(result);
                if (resGudao > 0)
                {
                    if (resPer > 0)
                    {
                        return 1; //操作成功
                    }
                    else
                    {
                        return -1;//修改工资数据失败
                    }
                }
                else
                {
                    return -2;//修改股道数据失败
                }
            }
            else
            {
                return -3;//没有找到对应车辆
            }

        }

///////////////////////////////////////////////////起票///////////////////////////////////////////////////////////////////////

        public int QiPiao(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyValue))
            {
                expression = expression.And(t => t.F_Id == keyValue);
                expression = expression.And(t => t.State == true);
                var result = service.FindEntity(expression);
                //if (string.IsNullOrEmpty(result.Fenhao))
                //{
                //    return -2;//封号为空不能起票
                //}
                if (result.isQiPiao !=true)
                {
                    result.isQiPiao = true;
                    result.QiPiaoTime = DateTime.Now;
                }
                else
                {
                    return -3; //返回已经起票不能再次进行起票操作！
                }
                //获取操作的车辆数据
                var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
                //获取当前上班的人员
                var positionrecord = positoonapp.GetForm(f => f.State == true);
                //插入操作数据
                OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
                oper.Create();
                oper.State = true;
                oper.OperationType = 1;
                oper.Banci = positionrecord.F_Id;
                oper.F_CreatorTime = DateTime.Now;
                oper.IsRollBack = false;
                var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
                oper.OperationPersion=new UserEntity();
                oper.OperationPersion = perO;
                result.QiPiaoOper = new OperationGudaoCarEntity();
                result.QiPiaoOper = oper;

                var resOperInsert = operService.Insert(oper);
                if (resOperInsert <= 0)
                {
                    return -4;//插入起票操作数据失败
                }
                var resGudaoUpdate = service.Update(result);
                if (resGudaoUpdate > 0)
                {
                    return 1;//成功
                }
                else
                {
                    return -5;//更新车辆数据失败
                }
               
            }
            else
            {
                return -1;//没有找到对应车辆
            }

           
        }

        ///////////////////////////////////////////取消起票////////////////////////////////////////////
        public int CancelQiPiao(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyValue))
            {
                expression = expression.And(t => t.F_Id == keyValue);
                expression = expression.And(t => t.State == true);
                var result = service.FindEntity(expression);
                //获取当前上班的人员
                var positionrecord = positoonapp.GetForm(f => f.State == true);
                var postInfo = posService.GetForm(f => f.F_Id == result.QiPiaoOper.Banci);
                if (positionrecord.BanciUser != postInfo.BanciUser)
                {
                    return -6;//返回 当前班次与起票时的班次不对应！不能进行此操作
                }
                if (result.IsAccomplishPermitThrough == true)
                {
                    return -2;//该车已放行！请先取消放行再进行取消起票操作！
                }
                if (result.isQiPiao == true)
                {
                    result.isQiPiao = false;
                    result.QiPiaoTime = DateTime.Now;
                }
                else
                {
                    return -3; //该车没有起票！不能进行取消起票操作！
                }
                //获取操作的车辆数据
                var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
              
                //插入操作数据
                OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
                oper.Create();
                oper.State = true;
                oper.OperationType =7;
                oper.Banci = positionrecord.F_Id;
                oper.F_CreatorTime = DateTime.Now;
                oper.IsRollBack = true;
                oper.RollbackTime=DateTime.Now;
                var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
                oper.OperationPersion = perO;
                result.QiPiaoOper = new OperationGudaoCarEntity();
                result.QiPiaoOper = oper;

                var resOperInsert = operService.Insert(oper);
                if (resOperInsert <= 0)
                {
                    return -4;//插入取消起票操作数据失败
                }
                var resGudaoUpdate = service.Update(result);
                if (resGudaoUpdate > 0)
                {
                    return 1;//成功
                }
                else
                {
                    return -5;//更新车辆数据失败
                }

            }
            else
            {
                return -1;//没有找到对应车辆
            }


        }

/////////////////////////////////////////////////////////////放行操作///////////////////////////////////
        /// <summary>
        /// </summary>
        /// <param name="keyValue">股道id</param>
        /// <param name="perKeyValue">货运员id</param>
        /// <returns></returns>
        public int Pass(string keyValue, string perKeyValue)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                return -1;//车辆信息不正确
            }
            if (string.IsNullOrEmpty(perKeyValue))
            {
                return -2;//请选择货运员
            }
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            //var LoginInfo = posService.GetForm(t => t.State == true);
            var expression = ExtLinq.True<GudaoCarEntity>();
            expression = expression.And(t => t.F_Id == keyValue);
            expression = expression.And(t => t.State == true);
            var result = service.FindEntity(expression);
            if (result.IsAccomplishPermitThrough == true)
            {
                return -3;//该车已经放行不能再次进行放行操作
            }
            else
            {
                result.IsAccomplishPermitThrough = true;
                result.AccomplishPermitThroughTime=DateTime.Now;
            }
            var pingming = servicepingming.FindEntity(f => f.Name == result.ThingsName && f.State == true);
            if (pingming != null && pingming.AllowFangxing == false) {
                return -40;//该车已经放行不能再次进行放行操作
            }
            //获取当前上班的人员
            var positionrecord = positoonapp.GetForm(f => f.State == true);
            //获取操作的车辆信息
            var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
            var person = userService.FindEntityAsNoTracking(f => f.F_Id == perKeyValue);//通过perKayValue 找到加钱的员工
            //插入操作数据
            OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
            oper.Create();
            oper.State = true;
            oper.OperationType = 4;
            oper.Banci = positionrecord.F_Id;
            oper.F_CreatorTime = DateTime.Now;
            oper.SalaryPeople=new UserEntity();
            oper.SalaryPeople = person; //给放行的人加工资
            oper.IsRollBack = false;
            //获取进行此操作的调度员
            var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
            oper.OperationPersion = perO;
            result.PassOper = new OperationGudaoCarEntity();
            result.PassOper = oper;
            //给货运员加工资
            PersonalSalaryEntity pSalary = new PersonalSalaryEntity();
            pSalary.Create();
            pSalary.Persion = person;
            if (pSalary.Persion.GongziDanjia == null)
            {
                return -6;//货运员的单价未填写！请联系管理员检查人员信息
            }
            pSalary.EveryTimeSalary =pSalary.Persion.GongziDanjia; //货运员工资等于工资单价*车辆数
            pSalary.EveryCarID = carriage;
            pSalary.F_CreatorTime = DateTime.Now;
            pSalary.OperationTime = DateTime.Now;
            pSalary.SalaryType = 1;
            pSalary.OperCarNum = 1;
            if (result.IsRecovery == true)
            {
                pSalary.Remake = "卸后利用";
            }
            pSalary.State = true;

            List<PersonalSalaryEntity> perList=new List<PersonalSalaryEntity>();
            perList.Add(pSalary);
            var resPSalary=perService.InsertPer(perList,oper);
            if (resPSalary <= 0)
            {
                return -4;//插入员工放行数据失败
            }
         
            var resGudao = service.Update(result);

            #region 跟新箱号信息（离开车站）
            Pagination pagination2 = new Pagination();
            pagination2.sord = "F_CreatorTime desc";
            pagination2.sidx = "F_CreatorTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            var carriageBoxs = carriageBoxservice.FindList(f => f.ChangeBelongCarriageId == carriage.CarriageID, pagination2);
            if (carriageBoxs != null && carriageBoxs.Count > 0)
            {
                carriageBoxs.ForEach(f =>
                {
                    f.LeaveTime = DateTime.Now;
                });
                carriageBoxservice.UpdateList(carriageBoxs);
            } 
            #endregion

            if (resGudao > 0)
            {
                return 1;//操作成功 
            }
            else
            {
                return -5;//修改股道数据失败
            }
        }

        /////////////////////////////////////////////////////取消放行///////////////////////////////////
        /// <summary>
        /// 取消放行
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public int CancelPass(string keyValue)
        {
            if (string.IsNullOrEmpty(keyValue))
            {
                return -1;//车辆信息不正确
            }
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            //var LoginInfo = posService.GetForm(t => t.State == true);
            var expression = ExtLinq.True<GudaoCarEntity>();
            expression = expression.And(t => t.F_Id == keyValue);
            expression = expression.And(t => t.State == true);
            var result = service.FindEntity(expression);
            //获取当前上班的人员
            var positionrecord = positoonapp.GetForm(f => f.State == true);
            var postInfo = posService.GetForm(f => f.F_Id == result.PassOper.Banci);
            if (positionrecord.BanciUser != postInfo.BanciUser)
            {
                return -2;//返回 当前班次与放行时的班次不对应！不能进行此操作
            }
            if (result.IsAccomplishPermitThrough != true)
            {
                return -3;//该车未放行！不能进行取消放行操作！
            }
            else
            {
                result.IsAccomplishPermitThrough = false;
                result.AccomplishPermitThroughTime=DateTime.Now;
            }
            
            //获取操作的车辆信息
            var carriage = carriageservice.FindEntityAsNoTracking(t => t.F_Id == result.BelongCarriage.F_Id);
            //找到放行操作加工资的人
            var oldPassOper = operService.FindEntity(f => f.F_Id == result.PassOper.F_Id);
            var person = userService.FindEntityAsNoTracking(f => f.F_Id == oldPassOper.SalaryPeople.F_Id);
            //插入操作数据
            OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
            oper.Create();
            oper.State = true;
            oper.OperationType = 9;
            oper.Banci = positionrecord.F_Id;
            oper.F_CreatorTime = DateTime.Now;
            oper.IsRollBack = true;
            oper.RollbackTime=DateTime.Now;
            //获取进行此操作的调度员
            var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
            oper.OperationPersion = perO;
            result.PassOper=new OperationGudaoCarEntity();
            result.PassOper = oper;

            //给货运员减工资
            PersonalSalaryEntity pSalary = new PersonalSalaryEntity();
            pSalary.Create();
            pSalary.Persion = person;
            pSalary.EveryTimeSalary = pSalary.Persion.GongziDanjia * -1; //货运员工资等于工资单价*车辆数
            pSalary.EveryCarID = carriage;
            pSalary.F_CreatorTime = DateTime.Now;
            pSalary.OperationTime = DateTime.Now;
            pSalary.SalaryType = 1;
            pSalary.OperCarNum = -1;
            if (result.IsRecovery == true)
            {
                pSalary.Remake = "卸后利用";
            }
            pSalary.State = true;
            List<PersonalSalaryEntity> perList = new List<PersonalSalaryEntity>();
            perList.Add(pSalary);
            var resPSalary = perService.InsertPer(perList, oper);
            
            if (resPSalary <= 0)
            {
                return -4;// 插入员工放行数据失败
            }
            var resGudao = service.Update(result);
            if (resGudao <= 0)
            {
                return -5;//更新车辆数据失败
            }
            else
            {
                return 1;
            }
           
        }

//////////////////////////////////////////////////卸后利用/////////////////////////////////////////////
        public int OnRecoveryCar(string keyValue)
        {
            //获取登录人员
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            var expression = ExtLinq.True<GudaoCarEntity>();
            expression = expression.And(t => t.F_Id == keyValue);
            expression = expression.And(t => t.State == true);
            var result = service.FindEntity(expression);
            if (result == null)
            {
                return -1;//没有获取到车辆信息
            }
            if (result.IsRecovery != true)
            {
                result.IsRecovery = true;
                result.isAccomplishStatistics = false;
                result.IsAccomplishduiwei = false;
                result.IsAccomplishPermitThrough = false;
                result.RecoveryTime = DateTime.Now;
            }
            else
            {
                return -2;//该车已经卸后利用不能再次进行该操作
            }
            //获取当前上班班次人员信息
            var positionrecord = positoonapp.GetForm(f => f.State == true);
            OperationGudaoCarEntity oper = new OperationGudaoCarEntity();
            oper.Create();
            oper.State = true;
            oper.OperationType = 11;
            oper.F_CreatorTime = DateTime.Now;
            oper.IsRollBack = false;
            //获取进行此操作的调度员
            var perO = userService.FindEntityAsNoTracking(f => f.F_Id == LoginInfo.UserId);
            oper.OperationPersion = perO;
            oper.Banci = positionrecord.F_Id;
            result.RecoveryOper = new OperationGudaoCarEntity();
            result.RecoveryOper = oper;
            
            var resOperInsert = operService.Insert(oper);
            if (resOperInsert <= 0)
            {
                return -3;//插入卸后利用操作数据失败
            }
            var resGudaoUpdate = service.Update(result);
            if (resGudaoUpdate > 0)
            {
                return 1;//成功
            }
            else
            {
                return -4;//更新车辆数据失败
            }
           
        }

        /// <summary>
        /// 放行查询的方法（不计算工资）
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public List<GudaoCarEntity> GetListFangXing(Pagination pagination, string keyword, string StartTime, string EndTime, string carId)

        {
            var expression = ExtLinq.True<GudaoCarEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.BelongPathway.Name.Contains(keyword));
            }

            if (!string.IsNullOrEmpty(carId))
            {
                expression = expression.And(t => t.BelongCarriage.CarriageID.Contains(carId));
            }
            if (!string.IsNullOrEmpty(StartTime))
            {
                DateTime sTime = Convert.ToDateTime(StartTime);
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.AccomplishPermitThroughTime >= sTime);
                    expression = expression.And(f => f.AccomplishPermitThroughTime < eTime);
                }
                else
                {
                    expression = expression.And(f => f.AccomplishPermitThroughTime >= sTime);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(EndTime))
                {
                    DateTime eTime = Convert.ToDateTime(EndTime);
                    expression = expression.And(f => f.AccomplishPermitThroughTime < eTime);
                }

            }
            //expression = expression.And(t => t.State == true);
            expression = expression.And(f => f.IsAccomplishPermitThrough == true);
            //expression = expression.And(f => f.QiPiaoOper.OperationType == 1);
            return service.FindList(expression, pagination);
        }
    }


    public class CarAndgudao
    {
        public string xuhao { get; set; }
        public string gudao { get; set; }
        public string chehao { get; set; }
        public string chexing { get; set; }
        public string zaizhong { get; set; }
        public string pingming { get; set; }
        public string fenghao { get; set; }
        public string daofazhan { get; set; }
        public string jianshu { get; set; }
        public string zhongliang { get; set; }
        public string kehu { get; set; }
        public string beizhu { get; set; }
    }
}
